package com.carcenter.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carcenter.common.page.MyPageData;
import com.carcenter.common.result.Result;
import com.carcenter.order.entity.CarBrands;
import com.carcenter.order.entity.Units;
import com.carcenter.order.service.IUnitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/units")
public class UnitsController {
    @Autowired
    private IUnitsService unitsService;

    //单位详情-     返回：单位表
    @GetMapping("/getDetail/{pageNum}/{pageSize}")
    public Result<?> getDetail(@PathVariable("pageNum") Integer pageNum,
                               @PathVariable("pageSize") Integer pageSize) {
        Page<Units> page = unitsService.getDetailList(pageNum,pageSize);
        MyPageData data = new MyPageData(page.getTotal(),page.getRecords());
        return Result.ok(data);
    }
    @GetMapping("/getUnits")
    public Result getUnits(){
        List<Units> list = unitsService.list();
        return Result.ok(list);
    }

    //新增单位-单位名称
    @PostMapping("/add")
    public Result<?> add(@RequestBody Units units) {
        unitsService.add(units);
        return  Result.ok();
    }

    //删除单位-id
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        unitsService.delete(id);
        return Result.ok();
    }

}
